﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ForrajeriaBCL;
using ForrajeriaDB;
using System.Data;
namespace ForrajeriaFRONT.Reportes
{
    public partial class ProveedorxForraje : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ProveedorBCL proBCL = new ProveedorBCL();
                    List<ProveedorMO> lista = new List<ProveedorMO>();
                 DataTable dt = new DataTable();
                try
                {
                    dt = proBCL.getProveedorReporte();
                    lista= proBCL.GetAll();
                }
                catch
                {
                    Response.Redirect("/Default.aspx");
                }
                cmbproveedor.Items.Add(new ListItem("---Todos---", "0"));

                foreach (ProveedorMO aux in lista)
                {
                    cmbproveedor.Items.Add(new ListItem(aux.RazonSocial, aux.CodigoProveedor.ToString()));
                }

                gridProveedor.DataSource = dt;
                gridProveedor.DataBind();
                ViewState["lista"] = dt;
                ViewState["columna"] = "";
                ViewState["direccion"] = "";
            }



        }

        protected void gridProveedor_PageIndexChanged(object sender, EventArgs e)
        {
            

        }

        protected void gridProveedor_Sorting(object sender, GridViewSortEventArgs e)
        {
    
           ViewState["columna"] = e.SortExpression;


           if (ViewState["direccion"].ToString() == "asc")
           {
               ViewState["direccion"] = "desc";
           }
           else
           {
               ViewState["direccion"] = "asc";
           }

           cargarGrilla();
          
           




        }

        protected void gridProveedor_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridProveedor.DataSource = (DataTable)ViewState["lista"];
           gridProveedor.PageIndex = e.NewPageIndex;
            gridProveedor.DataBind();

        }

        protected void cargarGrilla()
        {
            ProveedorBCL proBCL = new ProveedorBCL();
            DataTable dt = new DataTable();
          
            try
            {
                dt = proBCL.getProveedorReporte(txtForraje.Text, int.Parse(cmbproveedor.SelectedValue), ViewState["columna"].ToString(), ViewState["direccion"].ToString());
                    ViewState["lista"] = dt;
                    gridProveedor.DataSource = dt;
                    gridProveedor.DataBind();
                
            
            }
            catch
            {
                Response.Redirect("/Default.aspx");
            }
        }

        protected void btnFiltrar_Click(object sender, EventArgs e)
        {
            cargarGrilla();
        }
    }
}